package cn.lexi.vip;


import org.apache.commons.cli.*;
import org.apache.commons.lang.StringUtils;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.scheduling.annotation.EnableAsync;

import org.springframework.scheduling.annotation.EnableScheduling;


import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@ComponentScan(basePackages = {"cn.lexi.vip"})
@EntityScan(basePackages = {"cn.lexi.vip"})

@EnableAutoConfiguration
@EnableJpaRepositories(basePackages = {"cn.lexi.vip"})
@PropertySource({
        "classpath:application.properties",
        "classpath:lexi.properties"
})
@EnableTransactionManagement
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 24 * 60 * 60)
@EnableCaching
@EnableScheduling
@EnableAsync
@EnableAspectJAutoProxy
@Configuration
@SpringBootApplication
public class Bootstrap {

    public static void parseOptions(String[] args) throws ParseException {
        Options opt = new Options();
        Option worker = new Option("w", "worker", true, "total workers");
        worker.setRequired(true);
        Option currentWorker = new Option("cw", "currentWorker", true, "current worker");
        currentWorker.setRequired(true);
        opt.addOption("h", "help", false, "--worker=totalWorkers --currentWorker=currentWorker")
                .addOption(worker)
                .addOption(currentWorker);
        CommandLineParser parser = new DefaultParser();
        CommandLine commandLine = parser.parse(opt, args, true);
        if (!commandLine.hasOption("w") || !commandLine.hasOption("cw")) {
            System.out.println("must with --worker and --currentWorker args specified");
            System.exit(-1);
        }
        String value = commandLine.getOptionValue("worker");
        if (StringUtils.isNumeric(value) && StringUtils.isNotEmpty(value)) {
            Constants.Concurrent.WORKERS = Long.valueOf(value);
        } else {
            System.out.println("--worker must be a number");
            System.exit(-1);
        }
        value = commandLine.getOptionValue("currentWorker");
        if (StringUtils.isNumeric(value) && StringUtils.isNotEmpty(value)) {
            Constants.Concurrent.CURRENT_WORKER = Long.valueOf(value);
        } else {
            System.out.println("--currentWoker must be a number");
            System.exit(-1);
        }
    }

    public static void main(String[] args) throws Exception {
        parseOptions(args);
        SpringApplication.run(Bootstrap.class, args);
    }
}
